package com.kcnet.qk.controller.dynamic;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.constant.Const;
import com.kcnet.qk.constant.StringConst;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.FBStringUtil;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.jesse.nativelogger.NLogger;

/**
 * @Auther: 赵浩
 * @Date: 2018-04-27 15:34
 * @Description:
 */
public class TimeLineSendImgAndTextCTR extends BasePeopleController {
    private static TimeLineSendImgAndTextCTR releaseimgctr;
    private static String TAG = "发布图文";

    private String Send_Text = "测试图文消息文字";
    private List<String> imgList;
    private boolean isJoinNext;

    public static TimeLineSendImgAndTextCTR getInstance() {
        if (releaseimgctr == null) {
            synchronized (TimeLineSendImgAndTextCTR.class) {
                if (releaseimgctr == null) {
                    releaseimgctr = new TimeLineSendImgAndTextCTR();
                }
            }
        }
        return releaseimgctr;
    }

    @Override
    public void doTask(AccessibilityEvent event) {
        mClassName = (String) event.getClassName();
        mType = event.getEventType();
        if (!initFbMain && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBLauncherUI)) {
            NLogger.d(TAG, "1.执行发布动态文字 第1/4个功能选项");
            sleepTimedDefault();
            tagTextClick(FBStringUtil.getString(StringConst.TAB_DTXX));
            sleepTimedDefault();
            //tagSendTimeLine();
           // Onclic_shell("470", "450");
            RootShell.click(470,450);

            initFbMain = true;
        } else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.photos.simplepicker.launcher.SimplePickerLauncherActivity")) {
            NLogger.d(TAG, "2. 到达选择图片处");

        } else if (mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals("android.support.v7.widget.RecyclerView")
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals("android.support.v7.widget.RecyclerView")) {
            NLogger.d(TAG, "3.开始自动点击图片");
            begin();
        }
        else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.composer.activity.ComposerActivity")) {
            NLogger.d(TAG, "5. 达到发布界面 开始输入文字");
            // Onclic_shell("20", "233");
            RootShell.click(20, 210);
            NLogger.d("6.发布动态文字", "输入文本"+Send_Text);
            sleepTimedDefault();
            inputSayHiSentence(Send_Text);

            sleepTimedDefault();
            if(findTextClick(FBStringUtil.getString(StringConst.TIMELINE_XIAYIBU))){
                //如果是下一步
                tagTextClick(FBStringUtil.getString(StringConst.TIMELINE_XIAYIBU));
                isJoinNext = true;
            } else if(findTextClick(FBStringUtil.getString(StringConst.TIMELINE_FENXIANG))){
                tagTextClick(FBStringUtil.getString(StringConst.TIMELINE_FENXIANG));
               // stopTask();
            }else if(findTextClick(FBStringUtil.getString(StringConst.TIMELINE_FABU))){
                tagTextClick(FBStringUtil.getString(StringConst.TIMELINE_FABU));
              //  stopTask();
            }
        }
            else if (isJoinNext && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBIMMERUI)) {
                NLogger.d(TAG,"7. 发布动态图片 选择分享对象");
                if(RootShell.click(487,135)){
                    sleepTimedDefault();
                    RootShell.click(456,885);
                    stopTask();
                }
            }
        else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.4N6")) {
            NLogger.d(TAG, "6.点击立即分享");
            //tagTextClick("立即分享");
//            Onclic_shell("18", "888");
            RootShell.click(300,900);
            stopTask();
        }
    }


    public void tagSendTimeLine() {
        List<AccessibilityNodeInfo> list = getRoot().findAccessibilityNodeInfosByText("照片");
        if (!list.isEmpty()) {
            if (tagTextClickShell(list.get(0))) {

            }
        }
    }


    public void begin() {

        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, "android.support.v7.widget.RecyclerView");
                if (scrllorNodeInfos.size() > 0) {
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if (scrllorNodeInfo == null) {
                    return;
                }

                for (int q = 0; q < scrllorNodeInfo.getChildCount(); q++) {

                    if (currentAddCount == imgList.size()) {
                        currentAddCount = 0;
                        NLogger.d(TAG, "4.图片选择完成 点击完成");
                        tagTextClick(FBStringUtil.getString(StringConst.TIMELINE_WANCHENG));
                        break;
                    }
                    AccessibilityNodeInfo as = scrllorNodeInfo.getChild(q);
                    sleepTimedDefault();
                    currentAddCount++;
                    NLogger.d(TAG, "选择图片" + currentAddCount + " statsu:" + NodeUtil.performClick(as));

                    if (q == scrllorNodeInfo.getChildCount() - 1) {
                        if (scrllorNodeInfo != null) {
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }
                }

            }
        }
    }


    public void setParms(String content,List<String> list){
        //stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.Send_Text = content;
        this.imgList = list;
        isJoinNext = false;
        initFbMain = false;
    }

}
